package com.tanhua.sso.server.service;

import cn.hutool.core.util.StrUtil;
import com.tanhua.api.CommentsServiceApi;
import com.tanhua.api.UserInfoService;
import com.tanhua.config.UserHolder;
import com.tanhua.exceptionhander.CustomException;
import com.tanhua.result.Result;
import com.tanhua.sso.pojo.Comment;
import com.tanhua.sso.pojo.UserInfo;
import com.tanhua.sso.utils.CommentType;
import com.tanhua.sso.utils.RelativeDateFormat;
import com.tanhua.sso.vo.CommentVo;
import com.tanhua.util.Constants;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommentsService {

    @DubboReference
    private CommentsServiceApi commentsServiceApi;

    @DubboReference
    private UserInfoService userInfoService;

    @Autowired
    private AppMqMessageService appMqMessageService;

    @Autowired
    private AppUserFreezeService appUserFreezeService;

    //评论动态
    public Integer comment(Map<String,String> map) {
        appUserFreezeService.checkUserStatus("2",UserHolder.getUserId());
        //mq发送信息
        appMqMessageService.sendLogService(UserHolder.getUserId(),"0205","comment",null);
        //去空字符
        String comment = map.get("comment").trim();
        if (StrUtil.isEmpty(comment)){
            throw new CustomException("评论不能为空");
        }
        //封装
        Comment newComment=new Comment();
        //设置发布时间
        newComment.setCreated(System.currentTimeMillis());
        //设置类型
        newComment.setCommentType(CommentType.COMMENT.getType());
        //设置内容
        newComment.setContent(comment);
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //设置评论者id
        newComment.setUserId(userId);
        //设置发布id
        newComment.setPublishId(new ObjectId(map.get("movementId")));
        //新增评论
        Integer count=commentsServiceApi.save(newComment);
        return count;
    }

    /*查看评论列表*/
    public Result getComments(Integer page, Integer pagesize, String movementId) {
        //mq发送信息
        appMqMessageService.sendLogService(UserHolder.getUserId(),"0205","comment",null);
        //查看动态被评论信息
        List<Comment> commentList=commentsServiceApi.findCommentByMovementId(page,pagesize,movementId);
        List<CommentVo> commentVoList=new ArrayList<>();
        for (Comment comment : commentList) {
            //发评论者的id
            Long userId = comment.getUserId();
            CommentVo commentVo=new CommentVo();
            UserInfo userInfo = userInfoService.selectUser(userId);
            commentVo.setId(comment.getId().toHexString());
            commentVo.setContent(comment.getContent());
            commentVo.setAvatar(userInfo.getAvatar());
            commentVo.setNickname(userInfo.getNickname());
            commentVo.setCreateDate(RelativeDateFormat.format(new Date(comment.getCreated())));
            commentVo.setLikeCount(comment.getLikeCount());
            //获取当前用户的id
            Long nowId = UserHolder.getUserId();
            //判断当前用户是否点赞
            if (comment.getUserId().equals(nowId)){
                Integer like=commentsServiceApi.findMyLike(nowId,comment.getPublishId());
                commentVo.setHasLiked(like);
                commentVoList.add(commentVo);
            }
            commentVoList.add(commentVo);
        }
        int count = commentList.size();
        Integer pages=(count/pagesize)+1;
        return new Result(count,pagesize,pages,page,commentVoList);
    }
}
